﻿using System;
using UnityEngine;
using UnityEngine.UI;
using Utility;

// Token: 0x020003F5 RID: 1013
public class ScreenFade : MonoBehaviour
{
	// Token: 0x06001957 RID: 6487 RVA: 0x000A3E9C File Offset: 0x000A209C
	private void Awake()
	{
		this.clickBlocker = ResourceUtility.CreateInstance<GameObject>("CommonPrefabs/ClickBlocker");
		this.fadeImage = this.clickBlocker.GetComponentInChildren<Image>();
		this.rate = 0f;
		this.ChangeColor(0f);
		this.actionEnd = false;
	}

	// Token: 0x06001958 RID: 6488 RVA: 0x000A3EDC File Offset: 0x000A20DC
	private void LateUpdate()
	{
		this.timer += Time.deltaTime;
		if (!this.end)
		{
			if (this.next == ScreenFade.NEXT.ACTION)
			{
				this.next = ScreenFade.NEXT.NONE;
				this.action();
				return;
			}
			if (this.next == ScreenFade.NEXT.DIE)
			{
				this.next = ScreenFade.NEXT.NONE;
				this.end = true;
				this.Die();
				return;
			}
			this.rate = this.CalcRate();
			if (this.timer > this.delay)
			{
				if (this.action != null && !this.actionEnd)
				{
					if (this.type == ScreenFade.TYPE.OUT_IN && this.rate >= 0.5f)
					{
						this.next = ScreenFade.NEXT.ACTION;
						this.actionEnd = true;
					}
					else if (this.rate >= 1f)
					{
						this.next = ScreenFade.NEXT.ACTION;
						this.actionEnd = true;
					}
				}
				else if (this.rate >= 1f && this.suicide)
				{
					this.next = ScreenFade.NEXT.DIE;
				}
			}
		}
		this.ChangeColor(this.rate);
	}

	// Token: 0x06001959 RID: 6489 RVA: 0x000A3FFC File Offset: 0x000A21FC
	private void OnDestroy()
	{
		if (this.clickBlocker != null)
		{
			UnityEngine.Object.Destroy(this.clickBlocker);
		}
	}

	// Token: 0x0600195A RID: 6490 RVA: 0x000A401C File Offset: 0x000A221C
	public static ScreenFade Create()
	{
		return new GameObject("ScreenFade").AddComponent<ScreenFade>();
	}

	// Token: 0x0600195B RID: 6491 RVA: 0x000A4030 File Offset: 0x000A2230
	private void Setup(ScreenFade.TYPE type, Color color, float span, float delay, Action action, bool suicide)
	{
		this.type = type;
		this.setColor = color;
		this.span = span;
		this.delay = delay;
		this.action = action;
		this.suicide = suicide;
		this.end = false;
		this.ChangeColor(0f);
	}

	// Token: 0x0600195C RID: 6492 RVA: 0x000A407C File Offset: 0x000A227C
	private float CalcRate()
	{
		if (this.span == 0f)
		{
			return 1f;
		}
		if (this.timer < this.delay)
		{
			return 0f;
		}
		float num = this.timer - this.delay;
		return Mathf.Clamp01(num / this.span);
	}

	// Token: 0x0600195D RID: 6493 RVA: 0x000A40D4 File Offset: 0x000A22D4
	private void ChangeColor(float rate)
	{
		Color color = this.setColor;
		if (this.type == ScreenFade.TYPE.OUT_IN)
		{
			if (this.next == ScreenFade.NEXT.ACTION)
			{
				color.a = 1f;
			}
			else if (rate < 0.5f)
			{
				color.a = rate * 2f;
			}
			else
			{
				color.a = 1f - (rate - 0.5f) * 2f;
			}
		}
		else
		{
			float a = (this.type != ScreenFade.TYPE.IN) ? 0f : 1f;
			float b = (this.type != ScreenFade.TYPE.IN) ? 1f : 0f;
			color.a = Mathf.Lerp(a, b, rate);
		}
		this.fadeImage.color = color;
	}

	// Token: 0x0600195E RID: 6494 RVA: 0x000A41A0 File Offset: 0x000A23A0
	public void Die()
	{
		UnityEngine.Object.Destroy(base.gameObject);
	}

	// Token: 0x0600195F RID: 6495 RVA: 0x000A41B0 File Offset: 0x000A23B0
	public static ScreenFade StartFade(ScreenFade.TYPE type, Color color, float span, float delay = 0f, Action action = null, bool suicide = true)
	{
		ScreenFade screenFade = ScreenFade.Create();
		screenFade.Setup(type, color, span, delay, action, suicide);
		return screenFade;
	}

	// Token: 0x04001C9D RID: 7325
	private Color setColor = Color.black;

	// Token: 0x04001C9E RID: 7326
	private ScreenFade.TYPE type;

	// Token: 0x04001C9F RID: 7327
	private float span = 1f;

	// Token: 0x04001CA0 RID: 7328
	private float delay;

	// Token: 0x04001CA1 RID: 7329
	private float timer;

	// Token: 0x04001CA2 RID: 7330
	private int depth = -1000;

	// Token: 0x04001CA3 RID: 7331
	private Action action;

	// Token: 0x04001CA4 RID: 7332
	private bool actionEnd;

	// Token: 0x04001CA5 RID: 7333
	private bool suicide = true;

	// Token: 0x04001CA6 RID: 7334
	private bool end;

	// Token: 0x04001CA7 RID: 7335
	private GameObject clickBlocker;

	// Token: 0x04001CA8 RID: 7336
	private Image fadeImage;

	// Token: 0x04001CA9 RID: 7337
	private float rate;

	// Token: 0x04001CAA RID: 7338
	private ScreenFade.NEXT next;

	// Token: 0x020003F6 RID: 1014
	public enum TYPE
	{
		// Token: 0x04001CAC RID: 7340
		IN,
		// Token: 0x04001CAD RID: 7341
		OUT,
		// Token: 0x04001CAE RID: 7342
		OUT_IN
	}

	// Token: 0x020003F7 RID: 1015
	private enum NEXT
	{
		// Token: 0x04001CB0 RID: 7344
		NONE,
		// Token: 0x04001CB1 RID: 7345
		ACTION,
		// Token: 0x04001CB2 RID: 7346
		DIE
	}
}
